package baoxin.com.robotcoach;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.MotionEvent;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class WeakActivity extends BaseActivity {

    PieChart mPieChart;
    LineChart mLineChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weak);

        initPieView();

        initLineView();
        initLineData();
    }

    private void initLineView() {
        mLineChart = findViewById(R.id.mLineChart);
        Description description = new Description();
        description.setText("错误步骤趋势");
        mLineChart.setDescription(description);


        mLineChart.setNoDataText("暂无数据");
        mLineChart.setDrawBorders(true);
        mLineChart.setBorderColor(Color.GREEN);
        mLineChart.setBorderWidth(1f);

        mLineChart.setTouchEnabled(true);
        mLineChart.setDragEnabled(true);
        mLineChart.setScaleEnabled(false);
        mLineChart.setDoubleTapToZoomEnabled(false);



        mLineChart.animateXY(1000, 1000);

        Legend l = mLineChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setForm(Legend.LegendForm.CIRCLE);
        l.setTextSize(14);

        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setEnabled(true);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setLabelCount(6,true);
        xAxis.setTextSize(14);

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return "第"+String.valueOf((int)value)+"周";
            }
        });

        mLineChart.getAxisLeft().setDrawGridLines(false);
      //  mLineChart.getAxisRight().setDrawGridLines(false);
        mLineChart.getAxisRight().setGridColor(Color.GREEN);

    }

    private void initLineData() {
        ArrayList<Entry> pointsValues = new ArrayList<>();
        pointsValues.add(new Entry(1, 44));
        pointsValues.add(new Entry(2, 24));
        pointsValues.add(new Entry(3, 7));
        pointsValues.add(new Entry(4, 3));
        pointsValues.add(new Entry(5, 1));
        pointsValues.add(new Entry(6, 1));


        ArrayList<Entry> pointsValues1 = new ArrayList<>();
        pointsValues1.add(new Entry(1, 34));
        pointsValues1.add(new Entry(2, 14));
        pointsValues1.add(new Entry(3, 2));
        pointsValues1.add(new Entry(4, 1));
        pointsValues1.add(new Entry(5, 1));
        pointsValues1.add(new Entry(6, 1));


        LineDataSet lineDataSet = new LineDataSet(pointsValues, "档位练习");
        lineDataSet.setColor(Color.RED);//线的颜色
        lineDataSet.setCircleColor(Color.RED);//圆点的颜色
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(4f);
        lineDataSet.setDrawCircles(true);//画圆
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawValues(false);//坐标

//        LineDataSet lineDataSet2 = new LineDataSet(pointsValues1, "灯光练习");
//        lineDataSet2.setColor(Color.YELLOW);//线的颜色
//        lineDataSet2.setCircleColor(Color.YELLOW);//圆点的颜色
//        lineDataSet2.setLineWidth(1f);
//        lineDataSet2.setCircleRadius(4f);
//        lineDataSet2.setDrawCircles(true);//画圆
//        lineDataSet2.setDrawCircleHole(false);
//        lineDataSet2.setDrawValues(false);


        List<ILineDataSet> lines = new ArrayList<>();
        lines.add(lineDataSet);
        LineData lineData = new LineData(lines);
        mLineChart.setData(lineData);
    }


    private void initPieView() {
        //饼状图
        mPieChart = findViewById(R.id.mPieChart);
        Description description = new Description();
        description.setText("练习步骤错误次数");
        mPieChart.setDescription(description);
        mPieChart.setUsePercentValues(true);//自动计算百分比
        mPieChart.getDescription().setEnabled(false);
        //    mPieChart.setExtraOffsets(0, 0, 0, 0);
        mPieChart.setNoDataText("暂无数据");

        mPieChart.setDragDecelerationFrictionCoef(0.95f);
        //设置中间文字
        mPieChart.setCenterText(generateCenterSpannableText());

        mPieChart.setDrawHoleEnabled(true);
        mPieChart.setHoleColor(Color.WHITE);

        //中心圆的边框
        mPieChart.setTransparentCircleColor(Color.BLACK);
        mPieChart.setTransparentCircleAlpha(110);

        //半径
        mPieChart.setHoleRadius(58f);
        mPieChart.setTransparentCircleRadius(61f);

        mPieChart.setDrawCenterText(true);

        //第一项的开始角度，顺时针
        mPieChart.setRotationAngle(0);
        // 触摸旋转
        mPieChart.setRotationEnabled(true);
        //点击项半径增大
        mPieChart.setHighlightPerTapEnabled(true);
        //是否显示label文字
        mPieChart.setDrawEntryLabels(false);

        //变化监听
        //  mPieChart.setOnChartValueSelectedListener(this);

        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

        //图例
        Legend l = mPieChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        //图例x y间距
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);
        l.setTextSize(14);
        l.setFormSize(14);

        // 文本文字颜色、大小
        mPieChart.setEntryLabelColor(Color.WHITE);
        mPieChart.setEntryLabelTextSize(12f);

        initPieData();
    }

    //设置中间文字
    private SpannableString generateCenterSpannableText() {
        SpannableString s = new SpannableString("练习步骤错误次数");
        return s;
    }

    private void initPieData() {
        ArrayList<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(140, "错误操作一"));
        entries.add(new PieEntry(20, "错误操作二"));
        entries.add(new PieEntry(30, "错误操作三"));
        entries.add(new PieEntry(10, "错误操作四"));
        entries.add(new PieEntry(5, "错误操作五"));
        entries.add(new PieEntry(20, "错误操作六"));

        setPieData(entries);


    }

    private void setPieData(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "档位错误分析");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);
        //数据和颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        //百分比文字大小
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.WHITE);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //刷新
        mPieChart.invalidate();
    }
}
